<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/19 9:14
 * Desc: 会员积分
 */
class Model_Api_V2_Integral extends ORM
{
    // 首页任务label
    const INDEX_STRATEGY_LABEL = array(
        'sys_member_register',
        'sys_member_login',
        'sys_member_upload_head',
        'sys_member_bind_phone',
        'sys_member_bind_email',
        'sys_member_bind_qq',
        'sys_member_bind_sina_weibo',
        'sys_member_bind_weixin'
    );

    /**
     * 积分首页任务
     * @return array
     */
    public static function get_index_strategy()
    {
        //首页任务
        $label = self::INDEX_STRATEGY_LABEL;
        //积分任务
        return DB::select('id', 'title', 'label', 'value')
            ->from('jifen')
            ->where('label', 'in', $label)
            ->and_where('isopen', '=', 1)
            ->order_by('id', 'asc')->limit(3)
            ->execute()
            ->as_array();
    }

    /**
     * 新人任务
     * @param $mid
     * @return array
     */
    public static function get_newbie_strategy($mid)
    {
        //用户信息
        $member = DB::select('litpic', 'email', 'mobile')
            ->from('member')
            ->where('mid', '=', $mid)
            ->execute()
            ->current();

        $sys_member_upload_head = array(
            'icon' => 10,
            'complete' => $member['litpic'] && $member['litpic'] != Model_Member::member_nopic(),
            'bind' => 'editData'
        );

        $sys_member_bind_phone = array(
            'icon' => 11,
            'complete' => (bool)$member['mobile'],
            'bind' => 'bindPhone'
        );

        $sys_member_bind_email = array(
            'icon' => 12,
            'complete' => (bool)$member['email'],
            'bind' => 'bindMailbox'
        );

        //新会员任务
        $newbie = array(
            'sys_member_register' => array('icon' => 8, 'complete' => true),
            'sys_member_upload_head' => $sys_member_upload_head,
            'sys_member_bind_phone' => $sys_member_bind_phone,
            'sys_member_bind_email' => $sys_member_bind_email,
        );

        //新人任务
        $strategy  = array();
        $strategy1 = DB::select('id', 'title', 'label', 'value')
            ->from('jifen')
            ->where(DB::expr('label in ("' . implode('","', array_keys($newbie)) . '")'))
            ->and_where('isopen', '=', 1)
            ->order_by('id', 'asc')
            ->execute()
            ->as_array();
        foreach ($strategy1 as $v) {
            //是否完成任务
            $v['complete'] = $newbie[$v['label']]['complete'];
            if (!$v['complete']) {
                $strategy[] = $v;
            }
        }

        return $strategy;
    }

    /**
     * 每日任务
     * @param int $mid
     * @return array
     */
    public static function get_daily_strategy(int $mid)
    {
        //每日任务
        if ($mid) {
            //登录
            $daily = array('sys_member_login', 'sys_write_notes', 'sys_write_jieban', 'sys_write_wenda');
        } else {
            $daily = array('sys_write_notes', 'sys_write_jieban', 'sys_write_wenda');
        }

        $strategy = DB::select('title', 'label', 'id', 'value')
            ->from('jifen')
            ->where(DB::expr('label in("' . implode('","', $daily) . '")'))
            ->and_where('isopen', '=', 1)
            ->order_by('id', 'asc')
            ->execute()
            ->as_array();
        foreach ($strategy as &$v) {
            if ($v['label'] == 'sys_write_wenda') {
                $v['title'] = '提交问题';
                $v['brief'] = "成功提问一次可获赠{$v['value']}积分";
            } elseif ($v['label'] == 'sys_member_login') {
                $v['title']    = '登录送积分';
                $v['brief']    = "每天登录一次获赠{$v['value']}积分";
                $v['complete'] = 1;
            } elseif ($v['label'] == 'sys_write_notes') {
                $v['title'] = '发布游记';
                $v['brief'] = "发表一篇可获得{$v['value']}积分";
            } elseif ($v['label'] == 'sys_write_jieban') {
                $v['title'] = '发布结伴';
                $v['brief'] = "发布一次结伴可获赠{$v['value']}积分";
            }
        }
        if ($mid) {
            if (St_Functions::is_normal_app_install('member_sign') && Model_Jifen::check_policy_is_open(6)) {
                $sign       = DB::select()
                    ->from('member_sign')
                    ->where('member_id', '=', $mid)
                    ->and_where('sign_date', '=', strtotime(date('Y-m-d')))
                    ->execute()
                    ->current();
                $strategy[] = array(
                    'id' => '99997',
                    'title' => '我要签到',
                    'brief' => '每天签到获赠更多积分',
                    'label' => 'sign',
                    'complete' => $sign ? 1 : 0,
                );
            }
        }

        if (Model_Jifen::check_policy_is_open(1)) {
            $strategy[] = array(
                'id' => '99998',
                'title' => '预订产品',
                'brief' => '成功预订产品可获赠积分',
                'label' => 'order_product',
            );
        }

        if (Model_Jifen::check_policy_is_open(2)) {
            $strategy[] = array(
                'id' => '99999',
                'title' => '评论产品',
                'brief' => '成功评论可获赠积分',
                'label' => 'evaluate_product',
            );
        }

        return $strategy;
    }


}